package school;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SessionsClientServlet
 */
public class SessionsClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SessionsClientServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		JDBCOptionDAO jdbcOption = new JDBCOptionDAO();
		Set<Option> listCourses = jdbcOption.getSessionsByClient(Integer.parseInt(session.getAttribute("clientID").toString()));
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.println("<h2>Your List of the Courses</h2>");
		out.println("<table col=\"4\" border=\"1\"><tr>");
		out.println("<th width=\"100\">Code</th><th width=\"150\">Title</th><th width=\"200\">Sessions</th><th width=\"100\">Location</th></tr>");
		
		if(listCourses.isEmpty()){
			out.println("<tr><td colspan=\"4\">You have not yet registered any course.</td></tr>");
		}
		else{
			for(Option o : listCourses) {
				out.println("<tr>");
				out.println("<td align=\"center\">" + o.getCode() + "</td>");
				out.println("<td align=\"center\">" + o.getTitle() + "</td>");
				out.println("<td align=\"center\">" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");		
				out.println("<td align=\"center\">" + o.getLocation() + "</td>");
				out.println("</tr>");
			}
		}
		
		out.println("</table><br/><br/>");
	}

}
